/**
 * @description: 授权管理
 * @version: 0.0.1
 * @author: z..
 * @Date: 2023-06-12 18:16:39
 */

import { Inject, Controller, Post, Body, Get, Query } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { AuthCenterConfigService } from '../service/auth.center.config.service';
import { PageDTO, IdDTO } from "../dto/common";
import { CodeDTO, CodeTokenDTO, InsertDTO, UpdateDTO } from "../dto/auth.center";
// 自定义装饰器
import { NotLogin } from "../decorator/not.login";
import { NotAuth } from "../decorator/not.auth";
import { NotFormat } from "../decorator/not.format"


@Controller('/api/v1/auth')
export class AuthCenterConfig {
  @Inject()
  ctx: Context;

  @Inject()
  AuthCenterConfigService: AuthCenterConfigService;

  /**
   * @summary 获取钉钉授权地址
   * @description 获取钉钉授权地址
   * @router get /api/v1/auth/getUri
   * @request query code  *query
   * @response 200 baseResponse 返回结果
   */
  @NotLogin()
  @NotAuth()
  @Get('/getUri')
  async getDingTalkAuthUri(@Query() options: CodeDTO): Promise<any> {
    return await this.AuthCenterConfigService.getDingTalkAuthUri(options.code);
  }

  /**
   * @summary 授权完成获取用户信息
   * @description 授权完成获取用户信息
   * @router post /api/v1/auth/userData
   * @request body code  *query
   * @response 200 baseResponse 返回结果
   */
  @NotLogin()
  @NotAuth()
  @Post('/userData')
  async getUserData(@Body() options: CodeDTO): Promise<any> {
    return await this.AuthCenterConfigService.getUserData(options.code);
  }


  /**
   * @summary 授权code换取token并获取用户信息
   * @description 授权code换取token并获取用户信息
   * @router post /api/v1/auth/accessToken
   * @request body code  *query
   * @response 200 baseResponse 返回结果
   */
  @NotLogin()
  @NotAuth()
  @Post('/accessToken')
  async getUserAccessToken(@Body() options: CodeTokenDTO): Promise<any> {
    return await this.AuthCenterConfigService.getUserAccessToken(options);
  }

  /**
   * @summary 微信小程序登录，code换取openid
   * @description 微信小程序登录，code换取openid
   * @router post /api/v1/auth/wxopenid
   * @request body code  *query
   * @response 200 baseResponse 返回结果
   */
  @NotLogin()
  @NotAuth()
  @NotFormat()
  @Post('/wxopenid')
  async getWxopenid(@Body() options: CodeDTO): Promise<any> {
    return await this.AuthCenterConfigService.getWxopenid(options.code);
  }


  /**
   * @summary 新增授权数据
   * @description 新增授权数据
   * @router post /api/v1/auth/insertAuth
   * @request body code  *query
   * @response 200 baseResponse 返回结果
   */
  @Post('/insertAuth')
  async insertAuth(@Body() options: InsertDTO): Promise<any> {
    return await this.AuthCenterConfigService.insertAuth(options);
  }

  /**
  * @summary 获取授权列表
  * @description 获取授权列表
  * @router post /api/v1/auth/getAuthPages
  * @response 200 baseResponse 返回结果
  */
  @Post('/getAuthPages')
  async getAuthPages(@Body() options: PageDTO): Promise<any> {
    return await this.AuthCenterConfigService.getAuthPages(options);
  }

  /**
  * @summary 修改授权信息
  * @description 修改授权信息
  * @router post /api/v1/auth/updateAuth
  * @response 200 baseResponse 返回结果
  */
  @Post('/updateAuth')
  async updateAuth(@Body() options: UpdateDTO): Promise<any> {
    return await this.AuthCenterConfigService.updateAuth(options);
  }

  /**
  * @summary 删除授权信息
  * @description 删除授权信息
  * @router post /api/v1/auth/deleteAuth
  * @response 200 baseResponse 返回结果
  */
  @Post('/deleteAuth')
  async deleteAuth(@Body() options: IdDTO): Promise<any> {
    return await this.AuthCenterConfigService.deleteAuth(options.id);
  }

  //      莫凡统一授权页面接口

  /**
   * @summary 根据code查询授权回调信息
   * @description 根据code查询授权回调信息
   * @router get /api/v1/auth/getUri
   * @request query code  *query
   * @response 200 baseResponse 返回结果
   */
  @NotLogin()
  @NotAuth()
  @Get('/getConfigByCode')
  async getConfigByCode(@Query() options: CodeDTO): Promise<any> {
    return await this.AuthCenterConfigService.getConfigByCode(options.code);
  }

}
